package com.jqq.collections_;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author 姬倩倩
 * @version 1.0
 */
@SuppressWarnings({"all"})
public class Collections01 {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add("tom");
        list.add("smith");
        list.add("king");
        list.add("milan");
        System.out.println("list="+list);
        //排序
        //reverse(List):反转List中元素的顺序
        Collections.reverse(list);
        System.out.println("reverse="+list);
        //随机打乱顺序
        Collections.shuffle(list);
        System.out.println("suffle="+list);
        //自然排序
        Collections.sort(list);
        System.out.println("sort="+list);
        //按字符串大小排序
        Collections.sort(list, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                return ((String)o1).length() - ((String)o2).length();
            }
        });
        System.out.println("lengthSort="+list);
        //交换
        Collections.swap(list, 0, 1);
        System.out.println("swap01="+list);
        //最大值
        System.out.println("max="+Collections.max(list));
        Object max = Collections.max(list, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                return ((String) o1).length() - ((String) o2).length();
            }
        });
        System.out.println("max="+max);
        //频次
        System.out.println(Collections.frequency(list,"tom"));
        //copy
        ArrayList newlist = new ArrayList();
        for (int i = 0; i < 4; i++) {
            newlist.add("");
        }
        Collections.copy(newlist,list);//会抛出异常
        System.out.println("newlist="+newlist);
        //替换所有
        Collections.replaceAll(list, "tom", "汤姆");
        System.out.println(list);
    }
}
